/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Porsche911GT2RS;
import jgame.*;
import jgame.platform.*; 

/**
 *
 * @author Martin
 */
public class GraphSim1 extends JGEngine{

    private Porsche911GT2RS auto;
    private Pedal bremspedal;
    private Pedal gaspedal;

/** Tutorial example 1: a minimal program.  A "bare skeleton" program
 * displaying a moving text "hello world".
 *
 * In order to run as both applet and application, you need to define a main()
 * method (this is the entry point for an application) and a parameterless
 * constructor (this is the entry point for an applet).  We use a second
 * constructor with a size parameter to initialise the engine as an
 * application.
 */
    private static boolean absIsSet;
    private static boolean asrIsSet;


	public static void main(String [] args) {
		// We start the engine with a fixed window size (which happens to
		// be twice the size of the defined playfield, scaling the playfield
		// by a factor 2).  Normally, you'd want this size to be configurable,
		// for example by means of command line parameters.
		new GraphSim1(new JGPoint(1280,900));
	}

	/** The parameterless constructor is called by the browser, in case we're
	 * an applet. */
	public GraphSim1() {
		// This inits the engine as an applet.
		initEngineApplet(); 
	}

	/** We use a separate constructor for starting as an application. */
	public GraphSim1(JGPoint size) {
		// This inits the engine as an application.
		initEngine(size.x,size.y); 
	}

	/** This method is called by the engine when it is ready to intialise the
	 * canvas (for an applet, this is after the browser has called init()).
	 * Note that applet parameters become available here and not
	 * earlier (don't try to access them from within the parameterless
	 * constructor!).  Use isApplet() to check if we started as an applet.
	 */
	public void initCanvas() {
		// The only thing we need to do in this method is to tell the engine
		// what canvas settings to use.  We should not yet call any of the
		// other game engine methods here!
		setCanvasSettings(
			20,  // width of the canvas in tiles
			15,  // height of the canvas in tiles
			16,  // width of one tile
			16,  // height of one tile
			     //    (note: total size = 20*16=320  x  15*16=240)
			null,// foreground colour -> use default colour white
			JGColor.green,// background colour -> use default colour black
			null// standard font -> use default font
		);
	}

	/** This method is called when the engine has finished initialising and is
	 * ready to produce frames.  Note that the engine logic runs in its own
	 * thread, separate from the AWT event thread, which is used for painting
	 * only.  This method is the first to be called from within its thread.
	 * During this method, the game window shows the intro screen. */
	public void initGame()  {
		// We can set the frame rate, load graphics, etc, at this point. 
		// (note that we can also do any of these later on if we wish)
            defineMedia("media.tbl");
		setFrameRate(
			50,// 50 = frame rate, 50 frames per second
			2  //  2 = frame skip, skip at most 2 frames before displaying
			   //      a frame again
                        );
                         auto = new Porsche911GT2RS();
                         bremspedal = new Pedal(1.0, 0.05, 0.0);
                         gaspedal = new Pedal(1.0, 0.01, 0.0);
                         
                         // Hintergrund
                         //Medientabelle eingebaut, um die defineImages rauszuziehen und platz zu sparen
                         
                         setBGImage("strasse");
                        
                         
        }
	/** Game logic is done here.  No painting can be done here, define
	* paintFrame to do that. */
    public void doFrame() {
        /**************************************************
         ***************Gas und Bremse*********************
         **************************************************/
        bremspedal.step( getKey(KeyDown) );
        gaspedal.step( getKey(KeyUp) );
              
         /**************************************************
         ***************Traktionssteuerung*********************
         **************************************************/
        if (getKey('A')) { auto.setTraction(1.0);
                           setBGImage("strasse"); }
        if (getKey('S')) { auto.setTraction(0.7);
                           setBGImage("naesse");}
        if (getKey('D')) { auto.setTraction(0.3);
                           setBGImage("schnee");}
        if (getKey('F')) { auto.setTraction(0.1);
                           setBGImage("eis");} 
        if (getKey('R')) { auto.reset(); }
        if (getKey(',')) { 
        if(!absIsSet) { if(auto.getAbs()) auto.setAbs(false); else auto.setAbs(true);
                           absIsSet = true;
        }} 
        else {absIsSet = false;}
        
        if (getKey('.')) { if(!asrIsSet) {if(auto.getAsr()) auto.setAsr(false); else auto.setAsr(true); 
                           asrIsSet = true;}}
        else {asrIsSet = false;}
        auto.step(0.02, gaspedal.value(), bremspedal.value());
}
   
	/** Any graphics drawing beside objects or tiles should be done here.
	 * Usually, only status / HUD information needs to be drawn here. */
	public void paintFrame() {
		setColor(JGColor.black);
		
		drawString("speed......." + Math.round(auto.getSpeedInKmH()) + "km/h", 200, 10, -1);
                drawString("time........" + (int)auto.getTime() + "s", 200, 20, -1);
                drawString("Pos........." + (int)auto.getPos() + "m", 200, 30, -1);
                drawString("Level......." + auto.getLevel(), 200, 40, -1);
                drawString("Brake......." + auto.getBrake(), 200, 50, -1);
                drawString("ABS........." + auto.getAbs(), 200, 60, -1);
                drawString("ASR........." + auto.getAsr(), 200, 70, -1);
    
                // Regelt?
                setColor(JGColor.red);
                if(auto.absActive()) drawString("ABS regelt", 200, 90, -1);
                if(auto.asrActive()) drawString("ASR regelt", 200, 90, -1);
                if(auto.abflug())    drawString("Abflug", 200, 90, -1);
           
	} 
         
	public class MyAuto extends JGObject {

		/** Constructor. */
		MyAuto () {
			super("myobject",true, // name
				GraphSim1.this.random(0,pfWidth()),
				GraphSim1.this.random(0,pfHeight()), // position
				1, // collision ID
				null// name of sprite or animation to use.
				          // This should be the name as defined in defineImage
				          // or the media table.
			);
			// move in random direction.
			xspeed = random(-2,2);
			yspeed = random(-2,2);
		}

		/** Update the object. This method is called by moveObjects. */
		public void move() {
			// bounce off the borders of the screen, play a sound when
			// bouncing.
			if ( (x >  pfWidth()-16 && xspeed>0)
			||   (x <            0  && xspeed<0) ) {
				xspeed = -xspeed;
			//	playAudio("bounce");
			}
			if ( (y > pfHeight()-16 && yspeed>0)
			||   (y <            0  && yspeed<0) ) {
				yspeed = -yspeed;
			//	playAudio("bounce");
			}
			// Make pacman face left or right, depending on X direction.
			//if (xspeed < 0) setGraphic("myanim_l"); else setGraphic("myanim_r");
		}

	} }



